﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class Admin_Login : System.Web.UI.Page
{
    UsersController usersController = new UsersController();
    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Write(MD5Encrypt("123123"));
        //if(!string.IsNullOrEmpty(Convert.ToString(Session["login"])))
        //{
        //    Response.Redirect("Default.aspx");
        //}
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string username = txtUsername.Text;
        string password = txtPassword.Text;
        string convertPass = MD5Encrypt(password);
        DataTable user = usersController.loginAdmin(username, convertPass);
        if (user.Rows.Count>0)
        {
            Users u = new Users(user.Rows[0]);
            if(u.Password==convertPass)
            {
                Session["login"] = u.User_id;
                Response.Redirect("Default.aspx");
            }
            
        }
        else
        {
            Response.Write("<script>alert('Đăng nhập không thành công')</script>");
            return;
        }
    }
    public static string MD5Encrypt(string orgString)
    {
        MD5CryptoServiceProvider mdcsp = new MD5CryptoServiceProvider();
        byte[] b = System.Text.Encoding.UTF8.GetBytes(orgString);
        b = mdcsp.ComputeHash(b);
        StringBuilder s = new StringBuilder();
        foreach (byte by in b)
        {
            s.Append(by.ToString("x2").ToLower());
        }

        return s.ToString();
    }
}